package cn.jianyongjie.org.controller;

import cn.jianyongjie.basic.aoon.PreAuthorize;
import cn.jianyongjie.basic.utils.AjaxResult;
import cn.jianyongjie.basic.utils.LoginConText;
import cn.jianyongjie.basic.utils.QueryResult;
import cn.jianyongjie.org.domain.Department;
import cn.jianyongjie.org.query.DepartmentQuery;
import cn.jianyongjie.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @Description: TODO
* @Author: Jian Yong Jie
* @Version: 1.0
* @Date:  2022/4/22 9:50
*/
@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService iDepartmentService;


    /*
    * @Title: DepartmentController
    * @Description: 查询所有部门数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/25 12:55
    * @Parameters: * @param null
    * @Return
    */
    @GetMapping
    public List<Department> findDept(){
        return iDepartmentService.loadAll();
    }



    /*
    * @Title: DepartmentController
    * @Description: 根据id查询数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 9:55
    * @Parameters: * @param id
    * @Return cn.jianyongjie.org.domain.Department
    */
    // 使用restful风格，设置请求
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "根据id查询数据",value = "findById")
    public Department findDepartmentById(@ApiParam(name="ID属性",required = true) @PathVariable("id") long id){

        return iDepartmentService.loadById(id);
    }

    /*
    * @Title: DepartmentController
    * @Description: 分页查询
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 10:01
    * @Parameters: * @param
    * @Return cn.jianyongjie.basic.utils.QueryResult
    */
    @PostMapping
    public QueryResult<Department> finddepartments(@RequestBody DepartmentQuery departmentQuery, HttpServletRequest request){
        System.out.println(LoginConText.loginConText(request));
        return iDepartmentService.queryData(departmentQuery);
    }

    /*
    * @Title: DepartmentController
    * @Description: 根据id删除数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/22 12:30
    * @Parameters: * @param
    * @Return void
    */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "通过id删除数据",value = "dept:deleteById")
    public AjaxResult delDepartment(@PathVariable("id") Long id){
        try {
            // 根据id删除数据
            iDepartmentService.remove(id);
            // 返回Ajax
            return AjaxResult.success();
            
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @PutMapping
    public AjaxResult addOrUpd(@RequestBody Department department){
        try {
            // 根据id进行新增或者修改
            if (department.getId()==null){
                // 进行新增
                iDepartmentService.save(department);
            }else {
                // 进行修改
                iDepartmentService.update(department);
            }
            return AjaxResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /*
    * @Title: DepartmentController
    * @Description: 批量删除
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/23 12:32
    * @Parameters: * @param ids
    * @Return cn.jianyongjie.basic.utils.AjaxResult
    */
    @PatchMapping
    public  AjaxResult delDepartments(@RequestBody List<Long> ids){
        try {
            iDepartmentService.batchRemove(ids);
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error();
        }
    }

    /*
    * @Title: DepartmentController
    * @Description: 查询部门树数据
    * @Author: Jian Yongjie
    * @Version: 1.0
    * @Date:  2022/4/24 11:51
    * @Parameters: * @param null
    * @Return
    */
    @GetMapping("/findtree")
    public List<Department> findTree(){
        return iDepartmentService.findTree();
    }
}
